<?php
// 合并 XiunoPHP
!defined('APP_PATH') AND define('APP_PATH', dirname(dirname(__FILE__)).'/');
function_exists('set_magic_quotes_runtime') AND set_magic_quotes_runtime(0);
$dir = '../xiunophp/';
$combines = ['comm_helpers.php','db_mysql.class.php','db_mysqli.class.php','db_pdo_mysql.class.php','db_pdo_sqlite.class.php','cache_apc.class.php','cache_memcached.class.php','cache_mysql.class.php','cache_redis.class.php','cache_xcache.class.php','cache_yac.class.php','db.func.php','cache.func.php','image.func.php','array.func.php','xn_encrypt.func.php','misc.func.php'];
$s = '';
foreach ($combines as $fi){
    $fp = $dir.$fi;
    if(is_file($fp)){//exists?
        $s .= php_strip_whitespace($fp);
    }
}

$s = preg_replace('/^\s*\<\?php\s+(.+?)(\s*\?\>)?\s*$/s','$1',$s);//去除php标签首尾
$s = str_replace('?><?php','',$s);
$xiunophp = file_get_contents($dir.'xiunophp.php');
$before = '// hook xiunophp_include_before.php';
$after = '// hook xiunophp_include_after.php';
$pre = substr($xiunophp, 0, strpos($xiunophp, $before) + 1 + strlen($before));
$suffix = substr($xiunophp, strpos($xiunophp, $after));
$xiunophp_min = trim($pre)."\r\n\r\n".trim($s)."\r\n\r\n".trim($suffix);

//echo $xiunophp_min;exit;
/*
$p = '#//\shook\sxiunophp_include_before\.php(.*?)//\shook\sxiunophp_include_after\.php#ism';
$xiunophp_min = preg_replace($p, $s, $xiunophp);
*/

/*
$xiunophp_min = preg_replace(
'#//\shook\sxiunophp_include_before\.php(.*)//\shook\sxiunophp_include_after\.php#ism', 
'//\shook\sxiunophp_include_before.php'.$s.'//\shook\sxiunophp_include_after.php', 
$xiunophp);*/

file_put_contents($dir.'xiunophp.min.php', $xiunophp_min);

echo 'ok';